const app = getApp();

Page({
  data: {
    nickName: '',
    avatar  : '',
    token   : '',
    phone   : '',
    tel     : '',
    order_pay_num:0,
    order_confirm_num:0,
    order_comment_num:0,
    order_refund_num:0,
    isLogin : true,
  },
  onBlur(e) {
    this.setData({
      inputValue: e.detail.value,
    });
  },
  onLoad(){
    var that = this;
    my.setNavigationBar({
      title: '会员中心',
    });
  },
  onShareAppMessage() {
    return {
      title: '花礼鲜花玫瑰生日',
      desc: '花礼鲜花玫瑰生日',
      path: 'pages/todos/todos'
    };
  },
  memberInfo(token){
    var that = this;
    my.httpRequest({
      url: app.geturl+'/member/my-info',
      method: 'GET',
      data:{
        token:token
      },
      dataType: 'json',
      success: function(re) {
        console.info(re);
        if(re.data.code==0){
          that.setData({
            phone:re.data.data.Obj.mobile,
            order_pay_num:re.data.data.Obj.order_pay_num,
            order_confirm_num:re.data.data.Obj.order_confirm_num,
            order_comment_num:re.data.data.Obj.order_comment_num,
            order_refund_num:re.data.data.Obj.order_refund_num
          });
        }else if(re.data.msg=='请登录后重试'){
          app.memberLogin();
        }
      }
    });
  },
  onShow(){
    var that = this;
    that.checkLogin();
    my.httpRequest({
      url: app.geturl+'/feedback/get-tel',
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        that.setData({
          tel: res.data.data.tel
        });
      }
    });
  },
  onPullDownRefresh() {
    that.checkLogin();
    my.stopPullDownRefresh();
  },
  checkLogin(){
    my.showLoading({});
    var that = this;
    my.getAuthCode({
      scopes: ['auth_user'],
      success: (authcode) => {
        my.httpRequest({
          url: app.geturl+'/member/login-by-ali-sp',
          method: 'POST',
          data:{
            code:authcode.authCode
          },
          dataType: 'json',
          success: function(res) {
            console.info(res);
            if(res.data.msg=='登录成功'){                    
              my.setStorage({
                key: 'token',
                data: {
                  token:res.data.data.token,
                  time:600
                },
                success: function() {
                  my.hideLoading();
                  my.getAuthUserInfo({
                    success: (res) => {
                      if(res.avatar){
                        my.setStorage({
                          key: 'memberInfo',
                          data: {
                            avatar:res.avatar,
                            nickName:res.nickName,
                          },
                          success: function() {
                            console.info('写入成功');
                          }
                        });
                        that.setData({
                          avatar:res.avatar,
                          nickName:res.nickName,
                          isLogin:false
                        });
                      }
                    }
                  });
                  that.memberInfo(res.data.data.token);
                }
              });
            }else{
              my.hideLoading();
              my.removeStorage({
                key: 'token',
                success: function(){
                  my.alert({
                    content: '授权登录失败',
                  });
                }
              });
            }
          }
        });
      }
    });
    my.hideLoading();
  },
  add() {
    app.todos = app.todos.concat([{
      text: this.data.inputValue,
      compeleted: false,
    }]);

    my.navigateBack();
  },
  makePhoneCall(e) {
    my.makePhoneCall({ number: this.data.tel });
  },
  addressList(){
    my.navigateTo({ url: '../add-todo/address_list' });
  },
  cartList(){
    my.navigateTo({ url: '../add-todo/cart' });
  },
  orderList(e){
    var id = e.target.dataset.type;
    my.navigateTo({ url: '../add-todo/order?status='+id });
  },
  memberPhone(){
    my.navigateTo({ url: '../add-todo/member_phone' });
  },
  myList(){
    my.navigateTo({ url: '../todos/todos' });
  },
});
